<?php
class template
{
	private $tpl_dir;
	private $vars = array();//Template variable storage
	private $file;//Template filename
	
	public function __construct($dir = "../../") {
		if (!is_dir($dir))
			return FALSE;
		$this->tpl_dir = $dir;
		return true;
	}
	//Creates/changes template var value
	public function setVar($name, $value) {
		$this->vars[$name] = $value;
		return true;
	}

	//Creates/changes multiple template variables
	public function setVars($array) {
		//$this->vars = array_merge($this->vars, $array);
		foreach ($array as $key => $val) {
			$this->vars[$key] = $val;
		}
		return true;
	}

	public function setBlock($name, $array) {
		$this->vars[$name][] = $array;
		return true;
	}


	public function unsetVar($name) {
		unset($this->vars[$name]);
		return true;
	}
	public function clearVars() {
		$this->vars = array();
		return true;
	}

	private function checkVars() {
		global $GLOBALS;
		foreach ($GLOBALS as $gvar)
			if (isset($this->vars[$gvar]))
				return TRUE;
	
		return FALSE;
	}

	public function display($file, $clearVars = FALSE)
	{
		//if ($this->checkVars())
		//	die("<p><b>Tpl error:</b> Variable exist, overwrite!<p>");

		$this->file = $this->tpl_dir.$file;
		unset($file);
		
		if (!file_exists($this->file))
			die("<p><b>Tpl error: ".$this->file."</b> does not exist!<p>");
					
		extract($this->vars, EXTR_OVERWRITE); // create vars from data array
		
		ob_start(); // start output buffering:
		require $this->file; // parse the file.
		$r = ob_get_contents(); // get parsed text
		ob_end_clean(); // quietly end output buffering
		
		if ($clearVars)
			$this->vars = array();
		
		return $r;
	}
}
?>
